package com.qianfeng.zhangwei.popupwindow;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.popupwindow
 * @类 名： PopupHelper
 * @创 建人： zhangwei
 * @创建时间：2015-08-12 14:20
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class PopupHelper {
    private static PopupHelper helper;
    private static Context context;
    private PopupWindow popupWindow;

    public static void init(Context context) {
        PopupHelper.context = context;
    }

    private PopupHelper() {

    }

    public static PopupHelper getInstance() {
        if (helper == null) {
            helper = new PopupHelper();
        }
        return helper;
    }

    public PopupWindow getPopupWindow(View contentView, int width, int height) {
        PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(height);
        popupWindow.setWidth(width);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setContentView(contentView);
        return popupWindow;
    }

    public PopupWindow getPopupWindow(View contentView) {
        return getPopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void showAsDropDown(View view, View contentView) {
        popupWindow = getPopupWindow(contentView);
        popupWindow.showAsDropDown(view);
    }

    public void showAsDropDown(View view, int gravity, View contentView) {
        PopupWindow popupWindow = getPopupWindow(contentView);
        popupWindow.showAtLocation(view, gravity, 0, 0);
    }

    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }


}
